<?php
    include('include/header.inc.php');  // Call header
    include('include/func.inc.php');
    $_SESSION['page'] = 'event';
    $_SESSION['previous_page_register'] = $_SERVER['REQUEST_URI'];
    
    $previous_page = $_SESSION['previous_page_detailTheme'];

    echo'<div data-role="content">';
    
    //Check that the "theme" parameter exist in the url
    if(isset ($_GET['theme'])){
        
        //Current theme id
        $id_theme = $_GET['theme'];
        
        //Username of the current user
        $username = strtoupper($_SESSION['login']);
        
        $rfc = connectSAP();			

        //-- FUNCTION MODULE: Z_FM_M646_GET_THEMES --
        //We must know if the function really exists
        $fce_get_themes = saprfc_function_discover($rfc, "Z_FM_M646_GET_THEMES");
        if(!$fce_get_themes){
            echo "The function module has failed.";
            echo $rfc;
            exit;
        }
        
        //Pass import parameters
        saprfc_import ($fce_get_themes,"IV_THEME",$id_theme);

        //Pass table parameters
        saprfc_table_init ($fce_get_themes,"TAB_DETAILS_THEME");

        //Call and execute the function
        $rc_get_themes = saprfc_call_and_receive ($fce_get_themes);            
        if ($rc_get_themes != SAPRFC_OK){
            if ($rfc == SAPRFC_EXCEPTION )
                echo ("Exception raised: ".saprfc_exception($fce_get_themes));
            else
                echo ("Call error: ".saprfc_error($fce_get_themes));
            exit;
        }

        //Fetch the data from the internal tables
        $data_row = saprfc_table_rows ($fce_get_themes,"TAB_DETAILS_THEME");
        
        if ($data_row > 0){ //If the theme exist
            $DATARow = saprfc_table_read ($fce_get_themes,"TAB_DETAILS_THEME",1);
            
            //Current event id
            $id_event = htmlentities($DATARow['EVENT_ID']);
            
            //Variables to display the date in a correct format 
            $dateStart = substr($DATARow['STARTDATE'],-2).'.'.substr($DATARow['STARTDATE'],4,2).'.'.substr($DATARow['STARTDATE'],0,4);
            $dateEnd = substr($DATARow['ENDDATE'],-2).'.'.substr($DATARow['ENDDATE'],4,2).'.'.substr($DATARow['ENDDATE'],0,4); 
            $timeStart = substr($DATARow['STARTTIME'],0,2).':'.substr($DATARow['STARTTIME'],2,2);
            $timeEnd = substr($DATARow['ENDTIME'],0,2).':'.substr($DATARow['ENDTIME'],2,2);
            
            //Display details of a theme
            //Title
            echo '<h2>'. htmlentities($DATARow['TITLE_THEME']) .'</h2>';
            echo '<div class="tableauPage">';
                //Description
                if ($DATARow['DESCRIPTION']<>'')
                    echo '<p><b>'. $texts['description'][$_SESSION['lang']] .'</b> : '.htmlentities($DATARow['DESCRIPTION']).'</p>';
                //Display dates (one or more days)
                if($dateStart == $dateEnd)
                    echo '<p><b>'. $texts['date'][$_SESSION['lang']] .'</b> : '.$dateStart.' - '.$timeStart.' '.$texts['start_end_time'][$_SESSION['lang']].' '.$timeEnd.'</p>';
                else
                    echo '<p><b>'. $texts['dates'][$_SESSION['lang']] .'</b> : '. $texts['from'][$_SESSION['lang']] .' '.$dateStart.' '. $texts['to'][$_SESSION['lang']] .' '.$dateEnd.'</p>';
            
                // Location
                echo '<p><b>'. $texts['address'][$_SESSION['lang']] .'</b> :';
                $address = '';
                echo '<table>';
                    echo '<tr>';
                        echo '<td><div class="agenda">';
                            if ($DATARow['ROOM']<>''){
                                echo '<br/>'.htmlentities($DATARow['ROOM']);
                            }
                            if ($DATARow['ADDRESS']<>''){
                                echo '<br/>'.htmlentities($DATARow['ADDRESS']);
                                $address .= htmlentities($DATARow['ADDRESS']). ' ';
                            }
                            if ($DATARow['ZIPCODE']<>''){
                                echo '<br/>'.htmlentities($DATARow['ZIPCODE']). ' '.htmlentities($DATARow['CITY']);
                                $address .= htmlentities($DATARow['ZIPCODE']). ' '.htmlentities($DATARow['CITY']). ' ';
                            }
                            if ($DATARow['COUNTRY']<>''){
                                $country = htmlentities($DATARow['COUNTRY']);
                                $country = substr($country, 0, 1) . strtolower(substr($country, 1));
                                echo '<br/>'.$country;
                                $address .= $country;
                            }
                        echo '</div></td>';
                        echo '<td valign=center>';
                            echo '<span style="margin-left:80px;"><a href="http://maps.google.com/?q='.$address.'" target="_blank"><img src="picture/lens.png"/></a></span>'; 
                        echo '</td>';
                    echo '</tr>';
                echo '</table>';
                //Instructor
                if ($DATARow['INSTRUCTOR_FIRSTNAME']<>'')
                    echo '<p><b>'. $texts['instructor'][$_SESSION['lang']] .'</b> : '.htmlentities($DATARow['INSTRUCTOR_FIRSTNAME']).' '.htmlentities($DATARow['INSTRUCTOR_NAME']).'</p>';
                //Language
                $language = htmlentities($DATARow['ID_LANGUAGE']);
                if ($DATARow['ID_LANGUAGE']<>'')
                    echo '<p><b>'. $texts['language'][$_SESSION['lang']] .'</b> : '. $language .'</p>';
                //Method
                $method =  '<div class="agenda"><br/>' . str_replace(" ; ", "<br/>",htmlentities($DATARow['METHOD'])) . "</div>";
                if ($DATARow['METHOD']<>'')
                    echo '<p><b>'. $texts['method'][$_SESSION['lang']] .'</b> : '. $method .'</p>';
                //Goal
                $goal =  '<div class="agenda"><br/>' . str_replace(" ; ", "<br/>",htmlentities($DATARow['GOAL'])) . "</div>";
                if ($DATARow['GOAL']<>'')
                    echo '<p><b>'. $texts['goal'][$_SESSION['lang']] .'</b> : '. $goal .'</p>';
                //Agenda
                $agenda =  '<div class="agenda"><br/>' . str_replace(" ; ", "<br/>",htmlentities($DATARow['AGENDA'])) . "</div>";
                if ($DATARow['AGENDA']<>'')
                    echo '<p><b>'. $texts['agenda'][$_SESSION['lang']] .'</b> : '.$agenda .'</p>';
                //Notes
                $notes =  '<div class="agenda"><br/>' . str_replace(" ; ", "<br/>",htmlentities($DATARow['NOTES'])) . "</div>";
                if ($DATARow['NOTES']<>'')
                    echo '<p><b>'. $texts['notes'][$_SESSION['lang']] .'</b> : '. $notes .'</p>';
                //Target audience
                $target =  '<div class="agenda"><br/>' . htmlentities($DATARow['TARGET']) . "</div>";
                if ($DATARow['TARGET']<>'')
                    echo '<p><b>'. $texts['target_audience'][$_SESSION['lang']] .'</b> : '. $target .'</p>';
                //Thematic
                if($_SESSION['lang'] == 'de'){
                    $thematic =  '<div class="agenda"><br/>' . htmlentities($DATARow['THEMATIC_DE']) . "</div>";
                    if ($DATARow['THEMATIC_DE']<>'')
                        echo '<p><b>'. $texts['thematic'][$_SESSION['lang']] .'</b> : '. $thematic .'</p>';
                }
                else{
                    $thematic =  '<div class="agenda"><br/>' . htmlentities($DATARow['THEMATIC_FR']) . "</div>";
                    if ($DATARow['THEMATIC_FR']<>'')
                        echo '<p><b>'. $texts['thematic'][$_SESSION['lang']] .'</b> : '. $thematic .'</p>';
                }
                //Number people
                $min_people =  htmlentities($DATARow['MINPEOPLE']);
                $max_people =  htmlentities($DATARow['MAXPEOPLE']);
                echo '<p><b>'. $texts['number_people'][$_SESSION['lang']] .'</b> : '. $min_people .' '.$texts['start_end_time'][$_SESSION['lang']].' '.$max_people.'</p>';

                $dir_nom = 'picture/photos/images/';
                $dir = opendir($dir_nom) or die('Erreur de listage : le répertoire n\'existe pas'); // on ouvre le contenu du dossier courant
                $fichier= array();
                $dossier= array();

                while($element = readdir($dir)) {
                    if($element==$id_theme)
                        echo '<br/><div class="agenda"><a href="gallery.php?theme=' . $id_theme . '&event='.$id_event.'" rel="external"><img src="picture/camera.png"/></a></p></div>' ;
                }
                closedir($dir);

            //-- FUNCTION MODULE: Z_FM_M646_GET_PARTICIPANTS 
            //We must know if the function really exists
            $fce_get_participants = saprfc_function_discover($rfc, "Z_FM_M646_GET_PARTICIPANTS");
            if(!$fce_get_participants){
                echo "The function module has failed.";
                echo $rfc;
                exit;
            }    

            //Pass import parameters
            saprfc_import ($fce_get_participants,"IV_THEME",$id_theme);

            //Pass table parameters
            saprfc_table_init ($fce_get_participants,"TAB_MEMBERS");

            //Call and execute the function
            $rc_get_participants = saprfc_call_and_receive ($fce_get_participants);            
            if ($rc_get_participants != SAPRFC_OK){
                if ($rfc == SAPRFC_EXCEPTION)
                    echo ("Exception raised: ".saprfc_exception($fce_get_participants));
                else
                    echo ("Call error: ".saprfc_error($fce_get_participants));
                exit;
            }

            //Fetch the data from the internal tables
            $data_row = saprfc_table_rows ($fce_get_participants,"TAB_MEMBERS");
            
            //Display all participants if there is/are
            if ($data_row > 0){
                //Title of the table
                echo '<p><b>'. $texts['participants'][$_SESSION['lang']] .'</b></p>';
                echo '<table width="100%" cellspacing="0"><tr class="titreTableau">';
                    echo '<td>'. $texts['name'][$_SESSION['lang']] .'</td>';
                    echo '<td>'. $texts['firstname'][$_SESSION['lang']] .'</td>';
                    echo '<td>'. $texts['institution'][$_SESSION['lang']] .'</td>';
                echo '</tr>';

                //Details foreach participants
                for ($i=1; $i<=$data_row; $i++){
                    $DATARow = saprfc_table_read ($fce_get_participants,"TAB_MEMBERS",$i);
                    
                    //To display 2 different color in the table (even and odd lignes)
                    $rem = $i % 2;
                    if($rem == 0)
                        echo "<tr bgcolor='#D3D3D3'>";//A8D2FF
                    else
                        echo "<tr bgcolor='#FFFFFF'>";
                            echo '<td>'.htmlentities($DATARow['LASTNAME']).'</td>';
                            echo '<td>'.htmlentities($DATARow['FIRSTNAME']).'</td>';
                            echo '<td>'.htmlentities($DATARow['INSTITUTION']).'</td>';
                        echo "</tr>";
                }
                echo "</table>";
            }
            else //No participants
               echo'<p align="center">'. $texts['no_participant'][$_SESSION['lang']] .'</p>';
            
            //realease the function
            saprfc_function_free($fce_get_participants);

            //-- FUNCTION MODULE: Z_FM_M646_GET_DETAILS_EVENT --
            //Get the subscription date to compare with today date
            //To know if the subscription button must be actived
            //We must know if the function really exists
            $fce_get_details_event = saprfc_function_discover($rfc, "Z_FM_M646_GET_DETAILS_EVENT");
            if(!$fce_get_details_event){
                echo "The function module has failed.";
                echo $rfc;
                exit;
            }    

            //Pass import parameters
            saprfc_import ($fce_get_details_event,"IV_EVENT",$id_event);
            
            //Pass table parameters
            saprfc_table_init ($fce_get_details_event,"TAB_DETAILS_EVENT");

            //Call and execute the function
            $rc_get_details_event = saprfc_call_and_receive ($fce_get_details_event);            
            if ($rc_get_details_event != SAPRFC_OK){
                if ($rfc == SAPRFC_EXCEPTION )
                    echo ("Exception raised: ".saprfc_exception($fce_get_details_event));
                else
                    echo ("Call error: ".saprfc_error($fce_get_details_event));
                exit;
            }

            //Fetch the data from the internal tables
            $data_row = saprfc_table_rows ($fce_get_details_event,"TAB_DETAILS_EVENT");
            //Not need to do a test if ($data_row > 0) beacause the event is existing
            $DATARow = saprfc_table_read ($fce_get_details_event,"TAB_DETAILS_EVENT",1);
            
            //Variables to have the subscription date in a correct format
            $registerDateStart = substr($DATARow['STARTREG'],-2).'.'.substr($DATARow['STARTREG'],4,2).'.'.substr($DATARow['STARTREG'],0,4);
            $registerDateEnd = substr($DATARow['ENDREG'],-2).'.'.substr($DATARow['ENDREG'],4,2).'.'.substr($DATARow['ENDREG'],0,4);
            
            //To compare the registration dates with the current date
            $today = date('d.m.Y');
            $todayFormat = explode(".", $today); 
            $dateToday = $todayFormat[2].$todayFormat[1].$todayFormat[0];

            $registerStart = explode(".", $registerDateStart);
            $dateRegisterStart = $registerStart[2].$registerStart[1].$registerStart[0];

            $registerEnd = explode(".", $registerDateEnd);
            $dateRegisterEnd = $registerEnd[2].$registerEnd[1].$registerEnd[0];
            
            //realease the function
            saprfc_function_free($fce_get_details_event);

            //-- FUNCTION MODULE: Z_FM_M646_GET_REGISTRATION --
            //Test if the user is already registered
            //To know which subscription button must displayed

            //We must know if the function really exists
            $fce_get_registration = saprfc_function_discover($rfc, "Z_FM_M646_GET_REGISTRATION");
            if(!$fce_get_registration){
                echo "The function module has failed.";
                echo $rfc;
                exit;
            }    
          
            //Pass import parameters
            saprfc_import ($fce_get_registration,"IV_THEME", $id_theme);
            saprfc_import ($fce_get_registration,"IV_MEMBER", $username);

            //Pass table parameters
            saprfc_table_init ($fce_get_registration, "TAB_REGISTRATION");

            //Call and execute the function
            $rc_get_registration = saprfc_call_and_receive ($fce_get_registration);            
            if ($rc_get_registration != SAPRFC_OK){
                if ($rfc == SAPRFC_EXCEPTION )
                    echo ("Exception raised: ".saprfc_exception($fce_get_registration));
                else 
                    echo ("Call error: ".saprfc_error($fce_get_registration));
                exit;
            }

            //Fetch the data from the internal tables
            $data_row = saprfc_table_rows ($fce_get_registration,"TAB_REGISTRATION");
            
            //Variable to define the status of the registration
            $alreadyRegistered;
            //Variable can have the following status :
            //'n' = no registration
            //'r' = registered
            //'c' = cancelled
            //'w' = waiting
            
            if ($data_row > 0){//If the registration exist   
                $DATARow = saprfc_table_read ($fce_get_registration,"TAB_REGISTRATION",1);
                //Set the status and id
                $alreadyRegistered = $DATARow['STATUS'];
            }
            else //No registration
                $alreadyRegistered = 'n';
            
            //realease the function
            saprfc_function_free($fce_get_registration);
            
            //Paragraph where the suscription button is displayed
            echo '<p align="center">';
            if($alreadyRegistered == 'w')
                echo '</br>'.$texts['actually_waiting'][$_SESSION['lang']].'</br></br>';

            //Test if the subscription is open with the dates
            if(($dateRegisterStart <= $dateToday) && ($dateToday <= $dateRegisterEnd)){ //Open
                //Test if the registration already exist
                if($alreadyRegistered == 'r' || $alreadyRegistered == 'w')//Display unsubscribe button
                    echo '<a href="./unregister.php?theme='.$id_theme.'&event='.$id_event.'" data-role="button" data-inline="true">'. $texts['unregister_theme'][$_SESSION['lang']] .'</a>';
                else if($alreadyRegistered == 'n' || $alreadyRegistered == 'c')//Display subscribe button (new subsription)
                    echo '<a href="./register.php?theme='.$id_theme.'&event='.$id_event.'" data-role="button" data-inline="true">'. $texts['register_theme'][$_SESSION['lang']] .'</a>';
            }
            else if($dateRegisterStart >= $dateToday)//Will openning (display the start date to subscribe)
                echo '<b>'.$texts['register_from'][$_SESSION['lang']] .' '.$registerDateStart.'</b></br></br>';
            else //Closed
                echo '<b>'.$texts['register_closed'][$_SESSION['lang']] .'</b></br></br>'; 
            
            //Display button back
            echo '<a href="'.$previous_page.'" data-role="button" data-inline="true">'. $texts['back'][$_SESSION['lang']] .'</a></p>';
            echo'</div>';
        }
        else{ //These theme does not exist
            echo '<h2>'.$texts['error'][$_SESSION['lang']].'</h2>';
            echo '<div class="tableauPage">';
                echo '<p align="center">'.$texts['theme_not_found'][$_SESSION['lang']].'<br/><br/>';
                
                if(isset ($_GET['event']))//Redirect on the event (id get by the parameter in the url)
                    echo '<a href="./detailEvent.php?event='.$_GET['event'].'" data-role="button" data-inline="true">'.$texts['back'][$_SESSION['lang']].'</a>';
                else//Redirect on all events
                    echo '<a href="./events.php" rel="external" data-role="button" data-inline="true">'.$texts['back'][$_SESSION['lang']].'</a>';

            echo '</p></div>';
        }

        //realease the function and close the connection
        saprfc_function_free($fce_get_themes);
        saprfc_close($rfc);
    }
    else{ //The parameter "theme" does not exist in the url
        echo '<h2>'.$texts['error'][$_SESSION['lang']].'</h2>';
        echo '<div class="tableauPage">';
        // Only the "event" parameter exists in the url
        if(isset ($_GET['event'])){
            //Error : theme not found
            echo '<p align="center">'.$texts['theme_not_found'][$_SESSION['lang']].'<br/><br/>';
            echo '<a href="./detailEvent.php?event='.$_GET['event'].'" data-role="button" data-inline="true">'.$texts['back'][$_SESSION['lang']].'</a>';
        }
        else {
            //Error : event not found
            echo '<p align="center">'.$texts['event_not_found'][$_SESSION['lang']].'<br/><br/>';
            echo '<a href="./events.php" rel="external" data-role="button" data-inline="true">'.$texts['back'][$_SESSION['lang']].'</a>';
        }
        echo '</p></div>';
    }
    echo '</div>';
    include('include/footer.inc.php');  // Call footer
?>